package com.mm.serivce.mp.impl;

import com.mm.base.forest.WeChatPayClient;
import com.mm.domain.wechat.*;
import com.mm.vo.mp.PayOrderReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;


/**
 * 描述: 微信小程序支付接口
 *
 * @author: yanglin
 * @Date: 2021-08-25-9:47
 * @Version: 1.0
 */
@Slf4j
@Service
public class MiniPayServiceImpl {

    private final WeChatPayClient weChatPayClient;

    public MiniPayServiceImpl(WeChatPayClient weChatPayClient) {
        this.weChatPayClient = weChatPayClient;
    }

    /**
     * 微信小程序-统一下单接口
     *
     * @param req
     * @param orderReq
     * @return
     */
    public UnifiedOrderRep placeOrder(HttpServletRequest req, PayOrderReq orderReq) throws Exception {
        // 定义参数
        MiniPayInfo miniPayInfo = MiniPayInfo.getInstance();
        String reqXml = miniPayInfo.bulidPlaceOrderParams(
                req, null, orderReq.getOrderCode()
                , orderReq.getTotalFree(), orderReq.getOpenId());
        // 发起请求
        UnifiedOrderRep orderRep = weChatPayClient.unifiedOrder(reqXml, (s, forestRequest, forestResponse) -> {
            // 成功响应回调
            log.info("placeOrder 成功 , {} ", s);
        }, (e, forestRequest, forestResponse) -> {
            // 异常回调
            log.info("placeOrder 异常 , {} ", e.getMessage());
        });
        return orderRep;
    }

    /**
     * 微信小程序-订单查询
     *
     * @param orderReq
     * @return
     * @throws Exception
     */
    public OrderQueryRep queryOrder(PayOrderReq orderReq) throws Exception {
        // 定义参数
        MiniPayInfo miniPayInfo = MiniPayInfo.getInstance();
        String reqXml = miniPayInfo.bulidQueryOrCloseOrderParams(orderReq.getOrderCode());
        // 发起请求
        OrderQueryRep queryRep = weChatPayClient.orderQuery(reqXml, (s, forestRequest, forestResponse) -> {
            // 成功响应回调
            log.info("queryOrder 成功 , {} ", s);
        }, (e, forestRequest, forestResponse) -> {
            // 异常回调
            log.info("queryOrder 异常 , {} ", e.getMessage());
        });
        return queryRep;
    }

    /**
     * 微信小程序-订单关闭
     *
     * @param orderReq
     * @return
     * @throws Exception
     */
    public CloseOrderRep closeOrder(PayOrderReq orderReq) throws Exception {
        // 定义参数
        MiniPayInfo miniPayInfo = MiniPayInfo.getInstance();
        String reqXml = miniPayInfo.bulidQueryOrCloseOrderParams(orderReq.getOrderCode());
        // 发起请求
        CloseOrderRep orderRep = weChatPayClient.closeOrder(reqXml, (s, forestRequest, forestResponse) -> {
            // 成功响应回调
            log.info("closeOrder 成功 , {} ", s);
        }, (e, forestRequest, forestResponse) -> {
            // 异常回调
            log.info("closeOrder 异常 , {} ", e.getMessage());
        });
        return orderRep;
    }

    /**
     * 微信小程序-申请退款
     *
     * @param orderReq
     * @return
     * @throws Exception
     */
    public RefundRep applyForRefund(PayOrderReq orderReq) throws Exception {
        // 定义参数
        MiniPayInfo miniPayInfo = MiniPayInfo.getInstance();
        String reqXml = miniPayInfo.bulidRefundParams(
                orderReq.getOrderCode(), orderReq.getTotalFree(), orderReq.getRefundFree());
        // 发起请求
        RefundRep refundRep = weChatPayClient.applyForRefund(reqXml, (s, forestRequest, forestResponse) -> {
            // 成功响应回调
            log.info("applyForRefund 成功 , {} ", s);
        }, (e, forestRequest, forestResponse) -> {
            // 异常回调
            log.info("applyForRefund 异常 , {} ", e.getMessage());
        });
        return refundRep;
    }

    /**
     * 微信小程序-查询退款
     *
     * @param orderReq
     * @return
     * @throws Exception
     */
    public RefundQueryRep queryRefund(PayOrderReq orderReq) throws Exception {
        // 定义参数
        MiniPayInfo miniPayInfo = MiniPayInfo.getInstance();
        String reqXml = miniPayInfo.bulidQueryOrCloseOrderParams(orderReq.getOrderCode());
        // 发起请求
        RefundQueryRep orderRep = weChatPayClient.queryRefund(reqXml, (s, forestRequest, forestResponse) -> {
            // 成功响应回调
            log.info("queryRefund 成功 , {} ", s);
        }, (e, forestRequest, forestResponse) -> {
            // 异常回调
            log.info("queryRefund 异常 , {} ", e.getMessage());
        });
        return orderRep;
    }
}
